///////////////////////////////查询任意轨迹的方法群////////////////////////////////////////

var gpslineQueryDlg = null;

var resultArray;

var gpslineResultLayer;

var gpslineFeatureArray;

function initLishiguijiQueryDlg(){
	if(gpslineQueryDlg)
		return;
	var gpslineQueryHtml = '<div style="width:340px;height:200px;font-size:11px;font-family:微软雅黑;position:absolute;left:15px;top:40px;">'+
											'<div id="lineQueryConditionBar" style="position:absolute;left:0px;top:0px;width:340px;height:40px;">'+
												'<label>时间范围：</label><input id="lineTimeInputForm" type="text" style="width:120px;height:18px;" class="Wdate" onfocus="WdatePicker({startDate: \'00:00:00\',dateFmt:\'yyyy-MM-dd HH:mm:ss\',alwaysUseStartDate:true})"/> '+
												' — <input id="lineTimeInputTo" style="width:120px;height:18px;" type="text" class="Wdate" onfocus="WdatePicker({startDate:\'00:00:00\',dateFmt:\'yyyy-MM-dd HH:mm:ss\',alwaysUseStartDate:true})"/>'+
												'<br/>'+
												'<label>轨迹名称：</label><input id="lineNameInputTo" style="width:150px;height:15px;"/>'+
												'<input id="queryButton" type="button" value="确定" />'+
											'</div>'+
											'<div id="queryLineResultContent" style="width:330px;height:135px;border:1px solid #ABADB3;position:absolute;left:2px;top:55px;overflow-y:auto;overflow-x:hidden;">'+
												'<table id="queryLineResultTable" style="font-size:11px;width:330px;border-collapse: collapse;">'+
													'<tr>'+
														'<td class="queryTd">显示</td><td class="queryTd">序号</td><td class="queryTd">轨迹名称</td><td class="queryTd">轨迹时间</td>'+
													'</tr>'+
												'</table>'+
											'</div>'+
										'</div>';
	gpslineQueryDlg = $.dialog({
		title: '历史轨迹查询',
		content: gpslineQueryHtml,
		dlgDivId: "gpslineQueryId",
		resize: false,
		max: false,
		left:"250px",
		top:'100px',
		width:'340px',
		height:'200px',
		min:gpslineQueryDlgMinFunc,
		init:function(){
			//对按钮的监听
			$("#queryButton").click(queryLineClickFunction);
		},
		close:gpslineQueryDlgCloseFunc
	});
}

function gpslineQueryDlgMinFunc(){
	minDlgByAnimate(gpslineQueryDlg);
}

function gpslineQueryDlgCloseFunc(){
	closeDlgLogicFunc();
	gpslineQueryDlg = null;
}

function closeDlgLogicFunc(){
	if(gpslineResultLayer){
		map.removeLayer(gpslineResultLayer);
		gpslineResultLayer = undefined;
	}
	resultArray = [];
	gpslineFeatureArray = [];
}

//////点击确定按钮之后的函数
function queryLineClickFunction(){
	var startTime = $("#lineTimeInputForm").val();
	var endTime = $("#lineTimeInputTo").val();
	var name = $("#lineNameInputTo").val();
	closeDlgLogicFunc();
	gpslineResultLayer = new OpenLayers.Layer.Vector("gpslineResultLayer");
	map.addLayer(gpslineResultLayer);
	var obj = {startTime:startTime,endTime:endTime,name:name};
	var url = "queryLishiguiji.shtml";
	$.post(url,obj,queryLishiguijiCallback);
}

//查询之后的回调
function queryLishiguijiCallback(res){
	$(".gpsResultTr").remove();
	res = eval(res);
	resultArray = res;
	gpslineFeatureArray = new Array(res.length);
	for(var i = 0;i<res.length;i++){
		var name = res[i].name;
		var timeStr = (res[i].time.year+1900)+"年"+(res[i].time.month+1)+"月"+(res[i].time.date)+"日"+(res[i].time.hours)+"时"+(res[i].time.minutes)+"分"+(res[i].time.seconds)+"秒";
		var lonlatstr = res[i].lonlatstr;
		var appendHtmlStr = '<tr class="gpsResultTr">'+
										'<td class="queryTd"><input type="checkbox" class="gpslineCheck" id="'+i+'"/></td><td class="queryTd">'+(i+1)+'</td><td class="queryTd">'+name+'</td><td class="queryTd">'+timeStr+'</td>'+
										'</tr>';
		$("#queryLineResultTable").append(appendHtmlStr);
	}
	$(".gpslineCheck").click(gpslineCheckClick);
}

//点击checkbox的函数
function gpslineCheckClick(evt){
	var id = Number(evt.currentTarget.id);
	if(evt.currentTarget.checked){
		var obj = resultArray[id];
		var lonlatstr = obj.lonlatstr;
		var lonlatArray = lonlatstr.split(";");
		var feature = createGPSLine(lonlatArray);
		gpslineFeatureArray[id] = feature;
		gpslineResultLayer.addFeatures(feature);
		var x = Number(lonlatArray[0].split(",")[0]);
		var y = Number(lonlatArray[0].split(",")[1]);
		var lonlat = new OpenLayers.LonLat(x,y);
		map.setCenter(lonlat);
	}else{
		gpslineResultLayer.removeFeatures(gpslineFeatureArray[id]);
	}
}

